﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Approval;
using Humanizer;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Approval
{
    public class CreateLeaveApplicationCommendHandlers : IRequestHandler<CreateLeaveApplicationCommends, APIResult<LeaveApplications>>
    {

        private readonly IBaseRepository<LeaveApplications>  leaveapplicationRepository;
        private readonly IBaseRepository<Record> recordRepository;
        private readonly IMapper mapper;

        public CreateLeaveApplicationCommendHandlers(IBaseRepository<LeaveApplications> leaveapplicationRepository, IBaseRepository<Record> recordRepository, IMapper mapper)
        {
            this.leaveapplicationRepository = leaveapplicationRepository;
            this.recordRepository = recordRepository;
            this.mapper = mapper;
        }



        /// <summary>
        /// 添加申请表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<LeaveApplications>> Handle(CreateLeaveApplicationCommends request, CancellationToken cancellationToken)
        {
            try
            {

                APIResult<LeaveApplications> result = new APIResult<LeaveApplications>();
                result.Code = APIEnum.添加成功;
                result.Msg = "添加成功";
                #region 申请表
                LeaveApplications leaveapplication = new LeaveApplications();
                leaveapplication.ApplicationId = request.ApplicationId;
                leaveapplication.LeaveType = request.LeaveType;
                leaveapplication.StartTime = request.StartTime.AddHours(8);
                leaveapplication.EndTime = request.EndTime.AddHours(8);
                leaveapplication.Reason = request.Reason;

                leaveapplication.Status = 0; //默认为0，表示提交申请
                leaveapplication.DepId = request.DepId;
                leaveapplication.CreateTime = DateTime.Now;
                leaveapplication.DepheadsName = request.DepheadsName;
                leaveapplication.UserName = request.UserName;




                // 计算请假时长和总工时
                TimeSpan duration = leaveapplication.EndTime - leaveapplication.StartTime;
                long totalHours = (long)duration.TotalHours;
                leaveapplication.Leavetime = leaveapplication.EndTime.ToString("yyyy-MM-dd") + "至" + leaveapplication.StartTime.ToString("yyyy-MM-dd");
                leaveapplication.Totalhours = totalHours / 3;

                leaveapplication.IsDelete = false; //默认为false，表示未删除

                await leaveapplicationRepository.Add(leaveapplication);
                #endregion

                #region 记录表
                Record record = new Record();
                record.ApplicationId = leaveapplication.ApplicationId;
                record.ApprovalName = leaveapplication.UserName;

                /*  record.UserName = leaveapplication.UserName;
                  record.Approvalstatus= false; //默认为false，表示未通过*/
                 record.ApprovalCreatetime = DateTime.Now;
                 record.DepId = leaveapplication.DepId;
                 record.Reason = leaveapplication.Reason;
                record.ApplicationName = leaveapplication.UserName;
                 record.ApprovalName = leaveapplication.DepheadsName;
                 record.ApprovalOpinion = "审批中";
                record.ApproveStatus = 0;
                await recordRepository.Add(record);
                #endregion
                return await Task.FromResult(result);
            }
            catch (Exception e)
            {

                throw;
            }
        }
    }
}
